﻿using Autofac.Core;
using AutoMapper;
using CoreProject.DTO;
using CoreProject.IService;
using CoreProject.Model;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace CoreProject.Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class AuthorityController : ControllerBase
    {
        private readonly IMapper _mapper;
        private readonly IAuthorityService _service;

        public AuthorityController(IMapper mapper,IAuthorityService service)
        {
            _mapper = mapper;
            _service = service;
        }

        [HttpPost]
        public async Task<ResultHandle> Post([FromBody] AddAuth auth)
        {
          
            var bol = await _service.AddAuth(auth.rid, auth.ids);
            ResultHandle result = new ResultHandle();
            result.state = 200;
            result.code = bol ? "1" : "-1";
            result.msg = bol ? "添加成功!" : "添加失败!";
            return result;

        }

        [HttpGet]
        public async Task<ResultData<List<AuthHsDto>>> Get(int rid)
        {
            var data = await _service.GetAuthListByRid(rid);
            var list = _mapper.Map<List<AuthHsDto>>(data);
            ResultData<List<AuthHsDto>> result = new ResultData<List<AuthHsDto>>();
            result.code = "1";
            result.msg = "ok";
            result.state = 200;
            result.data = list;
            return result;
        }

    }
}
